<?php
namespace Admin\Models\res;

use Phalcon\Di;
use Helpers\CurlClient as HttpClient;
use Admin\Models\InterfaceService;

/**
 * Class Dictionaries 字典
 * @package Admin\Models\Question
 */
class SchoolBase extends InterfaceService
{
    private static $interfaceKey = 'schoolBaseUrl';
    const GET_SCHOOLS = '/api/v1/schools/';//分页获取学校
    const GET_SCHOOL_TEACHER = '/api/v1/schools/{schoolId}/teachers/';//查找学校老师
    const USER_FETCH = '/api/v1/users/fetch';//获取用户信息

    /**
     * #方法说明：题库数据层
     * # Created by wy
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * #方法说明：创建httpUrl
     * @param $httpUrl
     * @param array $paramArr
    # Created by wy
     */
    public static function createHttpUrl($httpUrl, $paramArr = array())
    {
        return parent::createInterfaceUrl($httpUrl, $paramArr, self::$interfaceKey);
    }

    /**
     * #方法说明：分页获取学校
     * @param array
    # Created by wy
     */
    public function getSchools($paramArr = array(), $urlArr = array())
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl(self::GET_SCHOOLS, $urlArr));
        $result = $http->get(null, $paramArr)->headerConvert();
        if ($result['statusCode'] == 200) {
            return $http->bodyConvert();
        } else {
            return array();
        }
    }

    /**
     * #方法说明：查找学校老师
     * @return array
    # Created by wy
     */
    public function getSchoolTeachers($paramArr = array(), $urlArr = array())
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl(self::GET_SCHOOL_TEACHER, $urlArr));
        $result = $http->get(null, $paramArr)->headerConvert();
        if ($result['statusCode'] == 200) {
            return $http->bodyConvert();
        } else {
            return array();
        }
    }

    /**
     * #方法说明：获取用户信息
     * @param array $paramArr
     * @param array $urlArr
     * @return array|bool|mixed|string
    # Created by wy
     */
    public function getFetch($paramArr = array(), $urlArr = array())
    {
        $data = array();
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl(self::USER_FETCH, $urlArr));
        $result = $http->json(null, $paramArr)->headerConvert();
        if (in_array($result['statusCode'], array(200))) {
            return $http->bodyConvert();
        } else {
            return $data;
        }
    }

    /**
     * #方法说明：获取用户的单条数据
     * @param int $userId
    # Created by wy
     */
    public function getBatchUserInfo($userIds = array(), $fields = array())
    {
        $user = new self;
        $usersData = $user->getFetch(array('userIds' => is_array($userIds) ? implode(',', $userIds) : $userIds), array('fields' => is_array($fields) ? implode(',', $fields) : $fields));
        return $usersData ? $usersData : array();
    }
}